#include "Common/Utils.h"
#include "Common/Macros.h"
#include "CommonGame/Com.h"
#include "CommonGame/GlobalData.h"
#include "Npc.h"
#include "Equipment.h"
#include "Level.h"

GlobalData* GlobalData::m_pInstance = NULL;

GlobalData::GlobalData()
{
}

GlobalData::~GlobalData()
{
	if (m_pInstance != NULL)
	{
		delete m_pInstance;
		m_pInstance = NULL;
	}
}

GlobalData* GlobalData::getInstance()
{
	if (m_pInstance == NULL)
	{
		m_pInstance = new GlobalData();
	}

	return m_pInstance;
}

bool GlobalData::doInitialize()
{
	bool success = false;

	do
	{
		if (!initNpcData()) break;
		if (!initEquipmentData()) break;
		if (!initLevelData()) break;

		success = true;
	} while (0);

	CCASSERT(success, "GlobalData::initialize error!!!");

	return success;
}

void GlobalData::doDestory()
{
	CLEAR_VECTOR(m_npcDataArray);
	CLEAR_VECTOR(m_equipmentDataArray);
	CLEAR_VECTOR(m_levelDataArray);
}

const TNpcData* GlobalData::getNpcData(const std::string& key)
{
	for (auto n : m_npcDataArray)
	{
		if (key == n->key)
		{
			return n;
		}
	}

	return nullptr;
}

bool GlobalData::initNpcData()
{
	DbRecordSetPtr pRecordSet = new DbRecordSet();
	int cnt = DbHelper::getInstance()->getRecord(DBTABLE::NPC::TABLE, pRecordSet);
	if (cnt <= 0)
	{
		CCLOG("GlobalData::initNpc : no record");
		return false;
	}

	for (unsigned i = 0; i < pRecordSet->size(); i++)
	{
		TNpcData* pData = new TNpcData();

		DbKVPtr pRecord = pRecordSet->at(i);

		pData->id = getValueToInt(pRecord, DBTABLE::NPC::COL_ID);
		pData->key = getValue(pRecord, DBTABLE::NPC::COL_KEY);
		pData->is_hostage = getValueToInt(pRecord, DBTABLE::NPC::COL_IS_HOSTAGE);
		pData->head_center_x = getValueToInt(pRecord, DBTABLE::NPC::COL_HEAD_CENTER_X);
		pData->head_center_y = getValueToInt(pRecord, DBTABLE::NPC::COL_HEAD_CENTER_Y);
		pData->head_diameter = getValueToInt(pRecord, DBTABLE::NPC::COL_HEAD_DIAMETER);

		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_STAND), pData->standDura, pData->standArray);
		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_STAND_ATTACK), pData->standAttackDura, pData->standAttackArray);
		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_STAND_DIE), pData->standDieDura, pData->standDieArray);

		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_LIE), pData->lieDura, pData->lieArray);
		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_LIE_ATTACK), pData->lieAttackDura, pData->lieAttackArray);
		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_LIE_DIE), pData->lieDieDura, pData->lieDieArray);

		getAnimation(getValueToInt(pRecord, DBTABLE::NPC::COL_RUN), pData->runDura, pData->runArray);

		m_npcDataArray.push_back(pData);
	}

	DbHelper::freeResultSet(pRecordSet);

	return true;
}

const TEquipmentData* GlobalData::getEquipmentData(int id)
{
	for (auto n : m_equipmentDataArray)
	{
		if (id == n->id)
		{
			return n;
		}
	}

	return nullptr;
}

bool GlobalData::initEquipmentData()
{
	DbRecordSetPtr pRecordSet = new DbRecordSet();
	int cnt = DbHelper::getInstance()->getRecord(DBTABLE::EQUIPMENT::TABLE, pRecordSet);
	if (cnt <= 0)
	{
		CCLOG("GlobalData::initEquipmentData : no record");
		return false;
	}

	for (unsigned i = 0; i < pRecordSet->size(); i++)
	{
		TEquipmentData* pData = new TEquipmentData();

		DbKVPtr pRecord = pRecordSet->at(i);

		pData->id = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_ID);
		pData->name = getValue(pRecord, DBTABLE::EQUIPMENT::COL_NAME);
		pData->type = (EquipmentType)getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_TYPE);
		pData->attack = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_ATTACK);
		pData->defence = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_DEFENCE);
		pData->speed = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_SPEED);
		pData->af = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_AF);
		pData->durable = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_DURABLE);
		pData->currency = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_CURRENCY);
		pData->price = getValueToInt(pRecord, DBTABLE::EQUIPMENT::COL_PRICE);
		pData->pic = getValue(pRecord, DBTABLE::EQUIPMENT::COL_PIC);

		m_equipmentDataArray.push_back(pData);
	}

	DbHelper::freeResultSet(pRecordSet);

	return true;
}

const TLevelData* GlobalData::getLevelData(int id)
{
	for (auto n : m_levelDataArray)
	{
		if (id == n->id)
		{
			return n;
		}
	}

	return nullptr;
}

bool GlobalData::initLevelData()
{
	DbRecordSetPtr pRecordSet = new DbRecordSet();
	int cnt = DbHelper::getInstance()->getRecord(DBTABLE::LEVEL::TABLE, pRecordSet);
	if (cnt <= 0)
	{
		CCLOG("GlobalData::initLevelData : no record");
		return false;
	}

	for (unsigned i = 0; i < pRecordSet->size(); i++)
	{
		TLevelData* pData = new TLevelData();

		DbKVPtr pRecord = pRecordSet->at(i);

		pData->id = getValueToInt(pRecord, DBTABLE::LEVEL::COL_ID);
		pData->name = getValue(pRecord, DBTABLE::LEVEL::COL_NAME);
		pData->difficult = getValueToInt(pRecord, DBTABLE::LEVEL::COL_DIFFICULT);
		pData->duration = getValueToInt(pRecord, DBTABLE::LEVEL::COL_DURATION);
		pData->enemy_cnt = getValueToInt(pRecord, DBTABLE::LEVEL::COL_ENEMY_CNT);
		pData->ini_enemy_cnt = getValueToInt(pRecord, DBTABLE::LEVEL::COL_INI_ENEMY_CNT);
		pData->max_enemy_cnt = getValueToInt(pRecord, DBTABLE::LEVEL::COL_MAX_ENEMY_CNT);
		pData->hostage_cnt = getValueToInt(pRecord, DBTABLE::LEVEL::COL_HOSTAGE_CNT);
		pData->enemy_hp = getValueToInt(pRecord, DBTABLE::LEVEL::COL_ENEMY_HP);
		pData->enemy_attack = getValueToInt(pRecord, DBTABLE::LEVEL::COL_ENEMY_ATTACK);
		pData->attack_ratio = getValueToFloat(pRecord, DBTABLE::LEVEL::COL_ATTACK_RATIO);
		pData->run_ratio = getValueToFloat(pRecord, DBTABLE::LEVEL::COL_RUN_RATIO);
		pData->follow_time = getValueToFloat(pRecord, DBTABLE::LEVEL::COL_FOLLOW_TIME);
		pData->hit_ratio = getValueToFloat(pRecord, DBTABLE::LEVEL::COL_HIT_RATIO);
		pData->reward_bullet = getValueToInt(pRecord, DBTABLE::LEVEL::COL_REWARD_BULLET);
		pData->reward_coin = getValueToInt(pRecord, DBTABLE::LEVEL::COL_REWARD_COIN);
		pData->recom_gun = getValueToInt(pRecord, DBTABLE::LEVEL::COL_RECOM_GUN);
		pData->recom_suit = getValueToInt(pRecord, DBTABLE::LEVEL::COL_RECOM_SUIT);
		pData->level_target = getValue(pRecord, DBTABLE::LEVEL::COL_LEVEL_TARGET);
		pData->level_scene = getValue(pRecord, DBTABLE::LEVEL::COL_LEVEL_SCENE);
		pData->level_story = getValue(pRecord, DBTABLE::LEVEL::COL_LEVEL_STORY);
		pData->level_activity = getValue(pRecord, DBTABLE::LEVEL::COL_LEVEL_ACTIVITY);

		m_levelDataArray.push_back(pData);
	}

	DbHelper::freeResultSet(pRecordSet);

	return true;
}
